package csu.web.mypetstore.web.servlet;

import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.domain.Cart;
import csu.web.mypetstore.service.CartService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet(name = "CartFormServlet", urlPatterns = {"/cartForm"})
public class CartFormServlet extends HttpServlet {
    private static final String CART_FORM = "/WEB-INF/jsp/cart/cart.jsp";
    private CartService cartService;
    public  CartFormServlet()
    {
        cartService=new CartService();
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session= req.getSession();
        Account loginAccount=(Account) session.getAttribute("loginAccount");
        if(loginAccount==null)
        {
            resp.sendRedirect("signonForm");
        }
        else
        {
            Cart cart=cartService.createCart(loginAccount.getUsername());
            session.setAttribute("cart",cart);

            session.setAttribute("action","viewingCart");

            req.getRequestDispatcher(CART_FORM).forward(req,resp);

        }

    }
}
